import axios from "axios";
import { layer } from '@layui/layui-vue';
const api = axios.create({
    baseURL: "https://m.asd257.top",
    // timeout: 5000,
    headers: {
        "Content-Type": "application/json;chartset=uft-8",
    }
})
api.interceptors.request.use(config => {
    let token = window.sessionStorage.getItem("token");
    if (token) {
        config.headers["Authorization"] = token;
    }
    return config;
}, error => {
    return Promise.reject(error);
});

api.interceptors.response.use(response => {
    if (response.status == 200) {
        let token = response.data.token || '';
        window.sessionStorage.setItem("token", token);
        if (response.data.code == 401) {
            layer.msg('登录信息已失效', { time: 1000 });
            window.sessionStorage.removeItem("token");
            window.sessionStorage.removeItem("uKey");
            window.localStorage.removeItem("lKey");
            setTimeout(() => {
                window.location.reload();
            }, 1000)
            return Promise.reject(error);
        } else {
            return response;
        }
    } else {
        return Promise.reject(error);
    }
}, error => {
    return Promise.reject(error);
});

export default api;